﻿using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microformats.Enums;
using System.Globalization;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System;
using System.Collections;

namespace Microformats.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:hCalendar runat=server></{0}:hCalendar>")]
    [PersistChildren(false)]
    [ParseChildren(ChildrenAsProperties = true)]
    public class hCalendar : WebControl, INamingContainer
    {
        public hCalendar()
        {
            Properties = new Collection<CalendarProperty>();
        }

        /// <summary>
        /// Gets or sets the tele communication.
        /// </summary>
        /// <value>The tele communication.</value>
        [Browsable(true)]
        [Editor(typeof(CollectionEditor), typeof(UITypeEditor))]
        [Category("Behavior")]
        [Description("Properties")]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public Collection<CalendarProperty> Properties
        {
            get
            {
                object o = ViewState["Properties"];
                return o != null ? (Collection<CalendarProperty>)o : new Collection<CalendarProperty>();
            }
            set
            {
                ViewState["Properties"] = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            foreach (CalendarProperty calendarProperty in Properties)
            {
                if (calendarProperty is Summary)
                {
                    Summary summary = calendarProperty as Summary;

                    writer.WriteLine();
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, summary.ClassName);
                    writer.RenderBeginTag(summary.HtmlTag);
                    writer.Write(summary.Value);
                    writer.RenderEndTag();
                }
                if (calendarProperty is Description)
                {
                    Description description = calendarProperty as Description;

                    writer.WriteLine();
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, description.ClassName);
                    writer.RenderBeginTag(description.HtmlTag);
                    writer.Write(description.Value);
                    writer.RenderEndTag();
                }
                else if (calendarProperty is Location)
                {
                    Location location = calendarProperty as Location;

                    location.Render(ref writer);
                }
                else if (calendarProperty is DateCollection)
                {
                    RenderDates(ref writer, (DateCollection)calendarProperty);
                }
                else if (calendarProperty is Url)
                {
                    Url url = calendarProperty as Url;

                    writer.WriteLine();
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, url.ClassName);
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, url.Value);
                    writer.RenderBeginTag(url.HtmlTag);
                    writer.Write(url.Value);
                    writer.RenderEndTag();
                }
                else if (calendarProperty is CalendarGeo)
                {
                    CalendarGeo geo = calendarProperty as CalendarGeo;

                    geo.Render(ref writer);
                }
            }

            base.RenderContents(writer);
        }

        private void RenderDates(ref HtmlTextWriter writer, DateCollection collection)
        {
            foreach (Date date in collection.Dates)
            {
                if (date is EndDate)
                {
                    writer.Write(" - ");
                }

                writer.WriteLine();
                writer.AddAttribute(HtmlTextWriterAttribute.Class, date.ClassName);
                writer.AddAttribute(HtmlTextWriterAttribute.Title, date.FormattedDateTime);
                writer.RenderBeginTag(date.HtmlTag == HtmlTextWriterTag.Unknown ? "abbr" : date.HtmlTag.ToString().ToLower());
                writer.Write(!string.IsNullOrEmpty(date.Format) ? string.Format(date.Format, date.DateTime) : date.DateTime.ToString("M/d/yyyy"));
                writer.RenderEndTag();
            }
        }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "vevent");

            base.RenderBeginTag(writer);
        }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
            base.RenderEndTag(writer);
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }
    }
}
